﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using INSTA.GestionEcole.DAL;
using Button = System.Web.UI.WebControls.Button;
using TextBox = System.Web.UI.WebControls.TextBox;

namespace INSTA.EcoleGestion.FORMS
{
    public partial class ProfilEtudiant : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {




            if (!IsPostBack)
            {




                //On récupere L'id tapé dans la page d'accueil
                string email = (string) Session["Email"];

                //On récupere les cours que le professeur a mis en reprenant les champs session correspondant
                string coursEtud = (string) Session["Cours"];

                using (var context = new GestionInstaEntities())
                {

                    //string s = LabelNom.Text;

                    {
                        var query = from p in context.Etudiant
                            where p.email == email
                            select p;

                        foreach (var etu in query)
                        {

                            LabelNom.Text = etu.Nom;
                            LabelPrenom.Text = etu.Prenom;
                            LabelEmail.Text = etu.email;
                            LabelTel.Text = etu.Tel;
                            LabelAdresse.Text = etu.Adresse;
                            LabelPromo.Text = etu.Promo;

                            //On parse etu.ID ens tring avant la requete car linq reconnait pas les méthodes pour parsé
                            string s = Convert.ToString(etu.ID);
                            //On cherche dans la classe Notes les identifiants qui sont les memes que l'ID de 
                            string nom = (string) Session["NomEtudiant"];
                            // l'étudiant concerné
                            var query2 = from n in context.Notes
                                where (n.Identifiant == s) & (etu.Nom) == nom
                                select n;

                            foreach (var note in query2)
                            {
                                // ListBox2.Items.Add(note.Commentaire+" + "+note.NoteEtudiant);
                            }
                            //On cherche dans la classe Cours les cours concerné les étudiants de la promo
                            var query3 = from co in context.Cours
                                where co.Promo == etu.Promo
                                select co;




                            foreach (var cours in query3)
                            {
                               // ListBox1.Items.Add(cours.NomDuCours);
                            }
                            var query4 = from nouv in context.Nouveaute
                                where nouv.Promo == etu.Promo
                                select nouv;

                            foreach (var nouveaute in query4)
                            {
                                // ListBox3.Items.Add(nouveaute.News);
                            }
                            string chemin = @"~\Images\" + etu.Nom + ".jpg";
                            // on récupère l'adresse de l'image qu'on affecte à la varaiable chemin (string) 
                            Image1.ImageUrl = chemin;

                        }




                        context.SaveChanges();

                    }

                }
                using (var context = new GestionInstaEntities())
                {
                    string recupID = "";
                    var queryEtu = from et in context.Etudiant
                        where et.email == LabelEmail.Text
                        select et;
                   

                    foreach (var etu in queryEtu)
                    {
                        recupID = Convert.ToString(etu.ID);
                    }

                    var querNote = (from n in context.Notes
                        where n.Identifiant == recupID
                        select new {n.NoteEtudiant, n.Type}).ToList();

                    GridView2.DataSource = querNote;
                    GridView2.DataBind();
                }

                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    if (Convert.ToInt32(GridView2.Rows[i].Cells[0].Text) < 10)
                    {
                        GridView2.Rows[i].Cells[0].BackColor = Color.Brown;
                        GridView2.Rows[i].Cells[0].ForeColor = Color.Black;
                        GridView2.Rows[i].Cells[1].BackColor = Color.Brown;
                        GridView2.Rows[i].Cells[1].ForeColor = Color.Black;
                    }
                    else
                    {
                        GridView2.Rows[i].Cells[0].BackColor = Color.LawnGreen;
                        GridView2.Rows[i].Cells[0].ForeColor = Color.Black;
                        GridView2.Rows[i].Cells[1].BackColor = Color.LawnGreen;
                        GridView2.Rows[i].Cells[1].ForeColor = Color.Black;
                    }
                
                }
                BindGrid();
               
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    string nomCours = GridView1.Rows[i].Cells[2].Text;
                    string[] tab = nomCours.Split('.');
                    string extension = tab[1];
                    ImageButton ib = (ImageButton) GridView1.Rows[i].Cells[0].Controls[0];
                    switch (extension)
                    {
                        case "doc":
                            ib.ImageUrl = "~/Images/word-icon.png";
                            break;
                        case "docx":                        
                            ib.ImageUrl = "~/Images/word-icon.png";
                            break;
                        case "xls":
                            ib.ImageUrl = "~/Images/excel_icon1.png";
                            break;
                        case "xlsx":
                            ib.ImageUrl = "~/Images/excel_icon1.png";
                            break;

                        case "pdf":
                           
                            ib.ImageUrl = "~/Images/pdf_mini.png";
                            break;

                        case "txt":
                            ib.ImageUrl = "~/Images/txt.jpg";
                            break;

                    }
                }
                /*   if (tab[1] == "txt")
                {
                   
                    ib.ImageUrl = "~/Images/save-as-2.png";
                }*/

            }

        }

        public void BindGrid()
        {




            using (var context = new GestionInstaEntities())
            {
                var query = (from p in context.Cours
                    where p.Promo == LabelPromo.Text
                    select new {p.Promo, p.NomDuCours}).ToList();

                
              

                GridView1.DataSource = query;
                
                //((Button)GridView1.Rows[1].Cells[1].Controls[0])="~/Images/save-as-2.png";//Cours

                GridView1.DataBind();
               
            }
        }

        private void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {

            // If multiple ButtonField column fields are used, use the
            // CommandName property to determine which button was clicked.

        }

        protected void ValidationBouton(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "More")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
               string nomCours=GridView1.Rows[index].Cells[2].Text;
                string[] tab = nomCours.Split('.');
                    /* if (tab[1] == "txt")
                {
                    ImageButton ib = (ImageButton) GridView1.Rows[index].Cells[0].Controls[0];
                    ib.ImageUrl ="~/Images/save-as-2.png";
                }*/ 
                using(var context = new GestionInstaEntities()){
                var query = from p in context.Cours
                        where p.NomDuCours == nomCours
                        select p;
                  
                    foreach (var co in query)
                    {
                     //   string pathString = System.IO.Path.Combine(@"C:\Users", "cours");
                        File.WriteAllBytes(@"C:\Users\Sisi\Documents\"+co.NomDuCours, co.cours);
                      
                        //ff.Write(co.cours, 0, co.cours.Length);
                       // FileStream f = new FileStream(".",FileMode.Open);
                      //  f.Write(co.cours,0,co.cours.Length);
                      //  ff.Close();
                      //  Response.BinaryWrite(co.cours);
                       
                        //   OpenFile(@"C:\Users\Sisi\Documents\" + nomCours);
                        //SFileStream stream = File.Open(@"C:\Users\Sisi\Documents\BEL-MAHI_p110_V1.docx", FileMode.Open);
                        //stream.Close();
                        // File.Open(@"c:\" + course.NomDuCours + ".txt", course.cours);
                        //File.OpenRead()
                        //this.Response.BinaryWrite(co.cours);
                        //myFile.write(buffer, 100);
                        Process proc = new Process();
                        proc.StartInfo = new ProcessStartInfo(@"C:\Users\Sisi\Documents\" + co.NomDuCours);
                        proc.Start();
                    }

                }
            }

        }

    }
}



            

